import sys

from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QHBoxLayout, QTextEdit, QWidget, QPushButton, QApplication, \
    QLabel, QMessageBox

from GoogleFreeTrans import Translator

i18n_obj = {

}


# インターフェース 0.2

class TTk():
    def __init__(self):
        self.transfer = None
        self.make_trans_cn()

    def make_trans_cn(self):
        try:
            self.transfer = Translator.translator(src='zh-CN', dest='ja')
        except AttributeError:
            # rebuild
            self.make_trans_cn()

    def trans2chinese(self, tans_jan: str) -> str:

        return self.transfer.translate(tans_jan)


class BoxJan(QMainWindow):
    def __init__(self, parents=None):
        super(BoxJan, self).__init__(parents)
        self.setWindowTitle('GBKからshift-jis')
        self.resize(300, 90)

        # self.tansformer = TTk()

        self.jis_label = QLabel('乱\n码\n文\n本')
        self.gbk_label = QLabel('正\n常\n文\n本')
        self.chi_label = QLabel('中\n文\n文\n本')

        self.layout_gbk = QHBoxLayout()

        self.gbk_text = QTextEdit()
        self.gbk_text.setText('嬚梩巓枀偲儔僀僒儞僩搰偺揱愢')
        self.gbk_to_shift = QPushButton()
        self.gbk_to_shift.setText('解码')
        self.gbk_to_shift.clicked.connect(self.gbk)
        self.layout_gbk.addWidget(self.jis_label)
        self.layout_gbk.addWidget(self.gbk_text)
        self.layout_gbk.addWidget(self.gbk_to_shift)
        self.gbk_weight = QWidget()
        self.gbk_weight.setLayout(self.layout_gbk)

        self.layout_jis = QHBoxLayout()
        self.jis_text = QTextEdit()
        self.jis_to_gbk = QPushButton()
        self.jis_to_gbk.setText('转码')
        self.jis_to_gbk.clicked.connect(self.jis)
        self.jis_weight = QWidget()
        self.layout_jis.addWidget(self.gbk_label)
        self.layout_jis.addWidget(self.jis_text)
        self.layout_jis.addWidget(self.jis_to_gbk)
        self.jis_weight = QWidget()
        self.jis_weight.setLayout(self.layout_jis)

        self.chi_text=QTextEdit()


        self.main_layout = QVBoxLayout()
        self.main_layout.addWidget(self.gbk_weight)
        self.main_layout.addWidget(self.jis_weight)

        # self.setLayout(self.main_layout)
        total_weight = QWidget()
        total_weight.setLayout(self.main_layout)
        self.setCentralWidget(total_weight)

    def gbk(self):
        gbk_mess = self.gbk_text.toPlainText()
        # print(gbk_mess)
        self.trans4jis(gbk_mess)

    def jis(self):
        jis_mess = self.jis_text.toPlainText()
        self.trans4gbk(jis_mess)

    def trans4jis(self, gbk_t: str):
        try:
            self.jis_text.setText(gbk_t.encode('gbk').decode('shift-jis'))
        except UnicodeDecodeError or UnicodeEncodeError as ue:
            self.show_err()

    def show_err(self):
        reply = QMessageBox.critical(self, 'エラー', '間違ったところがある、修正しなさい', QMessageBox.Yes)
        print(reply)

    def trans4gbk(self, jis_t: str):
        try:
            self.gbk_text.setText(jis_t.encode('shift-jis').decode('gbk'))
        except UnicodeDecodeError or UnicodeEncodeError as ue:
            self.show_err()

    def trans_chinese(self):
        pass


if __name__ == '__main__':
    app = QApplication(sys.argv)
    show = BoxJan()
    show.show()
    sys.exit(app.exec_())
